require 'net/http'
require 'uri'
require 'rubygems'
require 'hpricot'
require 'open-uri'

 
def getRedirect ourl
	#puts ourl
	Net::HTTP.get_response(URI.parse(ourl))['location']	
end
#puts getRedirect('http://www.taobangwang.cn/taoapi.aspx?pid=6366189&sid=12821814&c=40')

def getTable url
	 # load the RedHanded home page
	 doc = Hpricot(open(url))	 
	 table = doc/"#ctl00_ContentHolder_MySortUserControl_GridViewTaobao"
	 top = 0;
	 trs = (table/"tr")
	 trs.each do |tr|
		if top>10
			tr.inner_html= ' '
			next
		end
		ts= (tr/"/td,/th")
		for i in 0...ts.length
			ts[i].set_attribute :align,'left'
			#<td align="left" width="80">  3389 件 </td> 
			ts[i].innerHTML = ('<em class="top">'+ ts[i].innerHTML + '</em>') if i==4
			ts[i].set_attribute :style,'{display:none;}' if(i==6 or i==8 or i==5)
		end
		if top==0
			tr.innerHTML = ('<th width="40"><em class="top">排名</em></th>') + tr.innerHTML 
		else
			tr.innerHTML = ("<td><em class='top'>"+top.to_s()+"</em></td>") + tr.innerHTML 
		end
		top = top+1
	 end
	 (table/"//a").each do |a|
		href = 'http://www.taobangwang.cn/'+a.attributes['href']
		href = getRedirect(href).sub(/\d{8}/,'11575228')
		a.set_attribute :href,href
	 end
	 style = '''<style type="text/css">
td,th{border-bottom:1px dashed #CDCDCD;font-size:12px;}
a{text-decoration:none;font-weight:800;font-family:Tahoma,SimSun,Arial;color:#0044DD;}
.top{color:#FF6600;display:block;font-size:13px;font-weight:700;font-style:normal;}
</style>
'''
	 return style + '<table>'+table.html+'</table>'
end
#File.new("table.html", "w+").puts getTable('http://www.taobangwang.cn/qq.aspx')
File.new("table.html", "w+").puts getTable('http://www.taobangwang.cn/lady.aspx')
